<template>
	<view class="template-product tn-safe-area-inset-bottom">
		<!-- 顶部自定义导航 -->
		<tn-nav-bar fixed alpha customBack>
			<view slot="back" class='tn-custom-nav-bar__back' @click="goBack">
				<text class='icon tn-icon-left'></text>
				<text class='icon tn-icon-home-capsule-fill'></text>
			</view>
		</tn-nav-bar>

		<swiper class="card-swiper" :circular="true" :autoplay="true" duration="500" interval="5000"
			@change="cardSwiper">
			<swiper-item v-for="(item,index) in swiperList" :key="index" :class="cardCur==index?'cur':''">
				<view class="swiper-item image-banner">
					<image :src="item.url" mode="aspectFill" v-if="item.type=='image'"></image>
				</view>
			</swiper-item>
		</swiper>
		<view class="indication">
			<block v-for="(item,index) in swiperList" :key="index">
				<view class="spot" :class="cardCur==index?'active':''"></view>
			</block>
		</view>

		<view class="tn-margin">
			<view class="tn-flex tn-flex-row-between">
				<view class="justify-content-item tn-text-bold tn-text-xxl">
					{{res.title}}
				</view>
			</view>
			<view class="tn-flex tn-flex-row-between tn-margin-top">
				<view class="justify-content-item tn-text-bold tn-color-purplered">
					<text class="" style="font-size: 50rpx;">{{res.price}}</text>
					<text class="tn-text-sm">￥</text>
				</view>
				<view class="justify-content-item tn-color-gray tn-padding-top-xs">
					<view class="">已售 {{res.sellCount}}</view>
				</view>
			</view>
		</view>

		<!-- 边距间隔 -->
		<view class="tn-strip-bottom"></view>

		<view class="tn-margin">
			<view class="tn-flex tn-flex-row-between">
				<view class="justify-content-item tn-text-bold tn-text-xl">
					产品标签
				</view>
			</view>
		</view>

		<view class="">
			<view class="tn-tag-content tn-margin tn-text-justify">
				<view v-for="(item, index) in tagList" :key="index"
					class="tn-tag-content__item tn-margin-right tn-round tn-text-sm tn-text-bold"
					:class="[`tn-bg-${item.color}--light tn-color-${item.color}`]">
					<text class="tn-tag-content__item--prefix">#</text> {{ item.title }}
				</view>
			</view>
		</view>

		<!-- 边距间隔 -->
		<view class="tn-strip-bottom"></view>


		<view class="tn-margin">
			<view class="tn-flex tn-flex-row-between">
				<view class="justify-content-item tn-text-bold tn-text-xl">
					内容详情
				</view>
			</view>
		</view>

		<view class="content-backgroup tn-margin">
			<rich-text :nodes="res.describe"></rich-text>
		</view>

		<view class="tn-flex tn-flex-row-between" style="margin: 40rpx 0 60rpx 0;">
			<view class="tn-flex-1 justify-content-item tn-margin-xs tn-text-center">
				<tn-button backgroundColor="#ffaaff" padding="40rpx 0" width="90%" shadow fontBold open-type="contact">
					<text class="tn-icon-share-triangle tn-padding-right-xs tn-color-black"></text>
					<text class="tn-color-black">联系客服</text>
				</tn-button>
			</view>
			<view class="tn-flex-1 justify-content-item tn-margin-xs tn-text-center">
				<tn-button backgroundColor="#FFF00D" padding="40rpx 0" width="90%" shadow fontBold open-type="share">
					<text class="tn-icon-topics tn-padding-right-xs tn-color-black"></text>
					<text class="tn-color-black">分 享</text>
				</tn-button>
			</view>
			<view class="tn-flex-1 justify-content-item tn-margin-xs tn-text-center">
				<tn-button backgroundColor="#00FFC6" padding="40rpx 0" width="90%" shadow fontBold @click="byOrder">
					<text class="tn-icon-like-lack tn-padding-right-xs tn-color-black"></text>
					<text class="tn-color-black">立即购买</text>
				</tn-button>
			</view>

		</view>

		<view class='tn-tabbar-height'></view>

		<tn-popup v-model="popup.show" :marginTop="vuex_custom_bar_height" length="55%" :mode="popup.mode"
			:backgroundColor="popup.backgroundColor" :width="popup.width" :height="popup.height"
			:borderRadius="popup.borderRadius" :closeBtn="popup.closeBtn" :closeBtnIcon="popup.closeBtnIcon"
			:closeBtnPosition="popup.closeBtnPosition" :maskCloseable="popup.maskCloseable" @close="popup.closedPopup">
			<view class="popup-content" :class="{'popup-content--center': mode === 'center'}">



				<view class="tn-flex tn-flex-row-between tn-flex-col-center tn-padding-top-xl tn-margin">
					<view class="tn-flex justify-content-item">
						<view class="tn-bg-black tn-color-white tn-text-center"
							style="border-radius: 100rpx;margin-right: 8rpx;width: 45rpx;height: 45rpx;line-height: 45rpx;">
							<text class="tn-icon-empty-address" style="font-size: 30rpx;"></text>
						</view>
						<view class="tn-text-lg tn-padding-right-xs tn-text-bold">地址</view>
					</view>
					<tn-input v-model="address" type="select" placeholder="请选择地址" :border="border"
						@click="addressShow = true" inputAlign="right"></tn-input>
				</view>
				<tn-picker mode="region" v-model="addressShow" :params="params" @confirm="confirmPicker"
					placeholder="清选择地址"></tn-picker>

				<view class="tn-flex tn-flex-row-between tn-flex-col-center tn-padding-top-xl tn-margin">
					<view class="tn-flex justify-content-item">
						<view class="tn-bg-black tn-color-white tn-text-center"
							style="border-radius: 100rpx;margin-right: 8rpx;width: 45rpx;height: 45rpx;line-height: 45rpx;">
							<text class="tn-icon-map-fill" style="font-size: 30rpx;"></text>
						</view>
						<view class="tn-text-lg tn-padding-right-xs tn-text-bold">地址详情</view>
					</view>
					<tn-input placeholder="请填写具体地址" name="input" v-model="addressInfo" inputAlign="right"></tn-input>

				</view>

				<view class="tn-flex tn-flex-row-between tn-flex-col-center tn-padding-top-xl tn-margin">
					<view class="tn-flex justify-content-item">
						<view class="tn-bg-black tn-color-white tn-text-center"
							style="border-radius: 100rpx;margin-right: 8rpx;width: 45rpx;height: 45rpx;line-height: 45rpx;">
							<text class="tn-icon-my" style="font-size: 30rpx;"></text>
						</view>
						<view class="tn-text-lg tn-padding-right-xs tn-text-bold">联系人</view>
					</view>
					<tn-input placeholder="请填写联系人" name="input" v-model="canctUser" inputAlign="right"></tn-input>

				</view>

				<view class="tn-flex tn-flex-row-between tn-flex-col-center tn-padding-top-xl tn-margin">
					<view class="tn-flex justify-content-item">
						<view class="tn-bg-black tn-color-white tn-text-center"
							style="border-radius: 100rpx;margin-right: 8rpx;width: 45rpx;height: 45rpx;line-height: 45rpx;">
							<text class="tn-icon-tel" style="font-size: 30rpx;"></text>
						</view>
						<view class="tn-text-lg tn-padding-right-xs tn-text-bold">联系方式</view>
					</view>
					<tn-input placeholder="请填写联系方式" name="input" v-model="canctPhone" inputAlign="right"></tn-input>

				</view>

				<view class="tn-flex tn-flex-row-between tn-flex-col-center tn-padding-top-xl tn-margin">
					<view class="tn-flex justify-content-item">
						<view class="tn-bg-black tn-color-white tn-text-center"
							style="border-radius: 100rpx;margin-right: 8rpx;width: 45rpx;height: 45rpx;line-height: 45rpx;">
							<text class="tn-icon-tag" style="font-size: 30rpx;"></text>
						</view>
						<view class="tn-text-lg tn-padding-right-xs tn-text-bold">订单备注</view>
					</view>
					<tn-input placeholder="请填写备注" name="input" v-model="byRemark" inputAlign="right"></tn-input>

				</view>

				<view class="tn-flex tn-flex-row-between tn-footerfixed">
					<view class="tn-flex-1 justify-content-item tn-margin-xs tn-text-center">
						<tn-button backgroundColor="#00FFC6" padding="40rpx 0" width="90%" shadow fontBold
							@click="byGoods()">
							<text class="tn-icon-add tn-padding-right-xs tn-color-black"></text>
							<text class="tn-color-black">确 认 购 买</text>
						</tn-button>
					</view>
				</view>
			</view>
		</tn-popup>

	</view>
</template>

<script>
	import template_page_mixin from '@/libs/mixin/template_page_mixin.js'
	import request from '@/common/request.js'
	export default {
		name: 'TemplateProduct',
		mixins: [template_page_mixin],
		data() {
			return {
				popup: {
					show: false,
					mode: 'bottom',
					backgroundColor: '',
					width: '',
					height: '',
					borderRadius: 0,
					closeBtn: true,
					closeBtnIcon: 'close',
					closeBtnPosition: 'top-right',
					maskCloseable: true,
				},
				params: {
					province: true,
					city: true,
					area: true,
				},
				address: '',
				addressInfo: '',
				canctUser: '',
				canctPhone: '',

				addressShow: false,
				swiperList: [],
				tagList: [],
				res: {},
				byRemark: '',
			}
		},
		onLoad: function(option) {
			this.res = JSON.parse(decodeURIComponent(option.info))
		},
		mounted() {
			this.loadData();
		},
		methods: {
			byOrder() {
				let token = uni.getStorageSync("token") || null;
				if (token == null) {
					uni.showModal({
						title: '提示：',
						content: "请先登录！",
						success: function(res) {
							if (res.confirm) {
								uni.navigateTo({
									url: '/minePages/regist'
								});
							}
						}
					});
				} else {
					this.popup.show = true;
				}
			},
			loadData() {
				let user = uni.getStorageSync("userinfo");
				console.log(user)
				if(user.province != null){
					this.address = user.province +"-"+ user.city +"-"+ user.town;
				}
				this.addressInfo = user.addressInfo;
				
				this.canctUser = user.people || user.nickName;
				this.canctPhone = user.phone || user.phonenumber;
				let color = [{
					color: 'red'
				}, {
					color: 'cyan'
				}, {
					color: 'blue'
				}, {
					color: 'green'
				}, {
					color: 'orange'
				}, {
					color: 'purplered'
				}, {
					color: 'purple'
				}, {
					color: 'brown'
				}, {
					color: 'yellowgreen'
				}, {
					color: 'grey'
				}];
				let good = this.res;
				for (let i = 0; i < good.allImage.length; i++) {
					let temp = {};
					temp.type = 'image';
					temp.url = good.allImage[i];
					this.swiperList.push(temp);
				}
				let tag = good.tag.split(",")
				for (let i = 0; i < tag.length; i++) {
					let temp = {};
					var randomNumber = this.getRandomNumber(0, 9);
					temp.color = color[randomNumber].color;
					temp.title = tag[i];
					this.tagList.push(temp);
				}
			},
			getRandomNumber(min, max) {
				return Math.floor(Math.random() * (max - min + 1)) + min;
			},
			// cardSwiper
			cardSwiper(e) {
				this.cardCur = e.detail.current
			},
			// 跳转
			tn(e) {
				uni.navigateTo({
					url: e,
				});
			},
			getRandomCoolBg() {
				return this.$t.colorUtils.getRandomCoolBgClass()
			},
			byGoods() {
				let goods = this.res;

				request.post("/xxc/order", {
					goodsId: goods.goodsId,
					price: goods.price,
					byRemark: this.byRemark,
					address: this.address,
					addressInfo: this.addressInfo,
					canctUser: this.canctUser,
					canctPhone: this.canctPhone,
					state: 1
				}).then((res) => {
					if (res.msg === '余额不足') {
						uni.showModal({
							title: '提示：',
							content: res.msg,
							success: function(res) {
								if (res.confirm) {
									uni.reLaunch({
										url: '/minePages/recharge?goods=' + encodeURIComponent(
											JSON.stringify(goods)),
									});
								}
							}
						});
					} else {
						uni.showModal({
							title: '提示：',
							content: '客服尽快发货,耐心等待...',
							success: function(res) {
								if (res.confirm) {
									uni.reLaunch({
										url: '/discoveryPages/myGoodsOrder'
									})
								}
							}
						});
					}
				});

			},
			confirmPicker(event) {
				this.address = `${event.province.label}-${event.city.label}-${event.area.label}`
			},

		}
	}
</script>

<style lang="scss" scoped>
	.template-product {}

	.tn-tabbar-height {
		min-height: 120rpx;
		height: calc(140rpx + env(safe-area-inset-bottom) / 2);
	}

	/* 用户头像 start */
	.logo-image {
		width: 110rpx;
		height: 110rpx;
		position: relative;
	}

	.logo-pic {
		background-size: cover;
		background-repeat: no-repeat;
		// background-attachment:fixed;
		background-position: top;
		box-shadow: 0rpx 0rpx 80rpx 0rpx rgba(0, 0, 0, 0.15);
		border-radius: 10rpx;
		overflow: hidden;
		// background-color: #FFFFFF;
	}

	/* 胶囊*/
	.tn-custom-nav-bar__back {
		width: 100%;
		height: 100%;
		position: relative;
		display: flex;
		justify-content: space-evenly;
		align-items: center;
		box-sizing: border-box;
		background-color: rgba(0, 0, 0, 0.15);
		border-radius: 1000rpx;
		border: 1rpx solid rgba(255, 255, 255, 0.5);
		color: #FFFFFF;
		font-size: 18px;

		.icon {
			display: block;
			flex: 1;
			margin: auto;
			text-align: center;
		}

		&:before {
			content: " ";
			width: 1rpx;
			height: 110%;
			position: absolute;
			top: 22.5%;
			left: 0;
			right: 0;
			margin: auto;
			transform: scale(0.5);
			transform-origin: 0 0;
			pointer-events: none;
			box-sizing: border-box;
			opacity: 0.7;
			background-color: #FFFFFF;
		}
	}

	/* 轮播视觉差 start */
	.card-swiper {
		height: 750rpx !important;
	}

	.card-swiper swiper-item {
		width: 750rpx !important;
		left: 0rpx;
		box-sizing: border-box;
		// padding: 0rpx 30rpx 90rpx 30rpx;
		overflow: initial;
	}

	.card-swiper swiper-item .swiper-item {
		width: 100%;
		display: block;
		height: 100%;
		transform: scale(1);
		transition: all 0.2s ease-in 0s;
		overflow: hidden;
	}

	.card-swiper swiper-item.cur .swiper-item {
		transform: none;
		transition: all 0.2s ease-in 0s;
	}

	.image-banner {
		display: flex;
		align-items: center;
		justify-content: center;
	}

	.image-banner image {
		width: 100%;
		height: 100%;
	}

	/* 轮播指示点 start*/
	.indication {
		z-index: 9999;
		width: 100%;
		height: 36rpx;
		position: absolute;
		display: flex;
		flex-direction: row;
		align-items: center;
		justify-content: center;
	}

	.spot {
		background-color: #FFFFFF;
		opacity: 0.6;
		width: 10rpx;
		height: 10rpx;
		border-radius: 20rpx;
		top: -60rpx;
		margin: 0 8rpx !important;
		position: relative;
	}

	.spot.active {
		opacity: 1;
		width: 30rpx;
		background-color: #FFFFFF;
	}

	/* 间隔线 start*/
	.tn-strip-bottom-min {
		width: 100%;
		border-bottom: 1rpx solid #F8F9FB;
	}

	/* 间隔线 start*/
	.tn-strip-bottom {
		width: 100%;
		border-bottom: 20rpx solid rgba(241, 241, 241, 0.8);
	}

	/* 间隔线 end*/
	/* 标题 start */
	.nav_title {
		-webkit-background-clip: text;
		color: transparent;

		&--wrap {
			position: relative;
			display: flex;
			height: 120rpx;
			font-size: 46rpx;
			align-items: center;
			justify-content: center;
			font-weight: bold;
			background-image: url(https://resource.tuniaokj.com/images/title_bg/title44.png);
			background-size: cover;
		}
	}

	/* 标题 end */

	/* 底部tabbar start*/
	.footerfixed {
		position: fixed;
		width: 100%;
		bottom: 0;
		z-index: 999;
		background-color: #FFFFFF;
		box-shadow: 0rpx 0rpx 30rpx 0rpx rgba(0, 0, 0, 0.07);
	}

	/* 标签内容 start*/
	.tn-tag-content {
		&__item {
			display: inline-block;
			line-height: 45rpx;
			padding: 10rpx 30rpx;
			margin: 20rpx 20rpx 5rpx 0rpx;

			&--prefix {
				padding-right: 10rpx;
			}
		}
	}

	/* 标签内容 end*/

	/* 内容图 start */
	.content-backgroup {
		z-index: -1;

		.backgroud-image {
			border-radius: 15rpx;
			width: 100%;
		}
	}

	/* 内容图 end */

	/* 商家商品 start*/
	.tn-blogger-content {
		&__wrap {
			box-shadow: 0rpx 0rpx 50rpx 0rpx rgba(0, 0, 0, 0.07);
			border-radius: 20rpx;
			margin: 15rpx;
		}

		&__info {
			&__btn {
				margin-right: -12rpx;
				opacity: 0.5;
			}
		}

		&__label {
			&__item {
				line-height: 45rpx;
				padding: 0 10rpx;
				margin: 5rpx 18rpx 0 0;

				&--prefix {
					color: #E83A30;
					padding-right: 10rpx;
				}
			}

			&__desc {
				line-height: 35rpx;
			}
		}

		&__main-image {
			border-radius: 16rpx 16rpx 0 0;

			&--1 {
				max-width: 690rpx;
				min-width: 690rpx;
				max-height: 400rpx;
				min-height: 400rpx;
			}

			&--2 {
				max-width: 260rpx;
				max-height: 260rpx;
			}

			&--3 {
				height: 212rpx;
				width: 100%;
			}
		}

		&__count-icon {
			font-size: 24rpx;
			padding-right: 5rpx;
		}
	}

	.image-book {
		padding: 150rpx 0rpx;
		font-size: 16rpx;
		font-weight: 300;
		position: relative;
	}

	.image-picbook {
		background-size: cover;
		background-repeat: no-repeat;
		// background-attachment:fixed;
		background-position: top;
		border-radius: 15rpx 15rpx 0 0;
	}

	/* 毛玻璃*/
	.dd-glass {
		width: 100%;
		backdrop-filter: blur(20rpx);
		-webkit-backdrop-filter: blur(20rpx);
	}
</style>